package cn.tedu.flower.manage.content.service.impl;

import cn.tedu.flower.manage.common.ex.ServiceException;
import cn.tedu.flower.manage.common.pojo.vo.PageData;
import cn.tedu.flower.manage.common.web.ServiceCode;
import cn.tedu.flower.manage.content.dao.persist.repository.IAddressRepository;
import cn.tedu.flower.manage.content.pojo.entity.Address;
import cn.tedu.flower.manage.content.pojo.param.AddressAddNewParam;
import cn.tedu.flower.manage.content.pojo.param.AddressUpdateParam;
import cn.tedu.flower.manage.content.pojo.vo.AddressListItemVO;
import cn.tedu.flower.manage.content.service.IAddressService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class AddressServiceImpl implements IAddressService {
    @Autowired
    private IAddressRepository iAddressRepository;

    @Override
    public void addNew(AddressAddNewParam addressAddNewParam) {
        log.debug("开始处理【新增标签类别】的业务，参数：{}", addressAddNewParam);
        Address address = new Address();
        BeanUtils.copyProperties(addressAddNewParam, address);
        int rows = iAddressRepository.insert(address);
        log.debug("添加地址行数", rows);
        String message = "添加成功";
        throw new ServiceException(ServiceCode.OK, message);
    }

    /**
     * @param id
     * @return
     */
    @Override
    public void deleteById(Long id) {
        log.debug("开始处理【删除标签】的业务，参数：{}", id);
        iAddressRepository.deleteById(id);
        String message = "添加成功";
        throw new ServiceException(ServiceCode.OK, message);
    }

    /**
     * @param addressUpdateParam
     * @return
     */
    @Override
    public void updateById(AddressUpdateParam addressUpdateParam) {
        Address address = new Address();
        BeanUtils.copyProperties(addressUpdateParam, address);
        int rows = iAddressRepository.insert(address);
        log.debug("添加地址行数", rows);
        String message = "修改成功";
        throw new ServiceException(ServiceCode.OK, message);

    }

    /**
     * @param username
     * @return
     */
    @Override
    public AddressListItemVO getAddressByUsername(String username) {
        log.debug("开始处理【根据Username查询地址】的业务，参数：{}", username);
        AddressListItemVO addressListItemVO = iAddressRepository.getAddressByUsername(username);
        if (addressListItemVO == null) {
            String message = "查询类别详情失败，类别数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return addressListItemVO;
    }

    /**
     * @param
     * @return
     */
    @Override
    public PageData<AddressListItemVO> getAddressAll(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询标签列表】的业务，页码：{}", pageNum);
        PageData<AddressListItemVO> pageData = iAddressRepository.getAddressAll(pageNum, pageSize);
        return pageData;
    }

    /**
     * @param id
     * @param isDefault
     * @return
     */
    @Override
    public void updateUpDefaultById(Long id, Integer isDefault) {
        log.debug("开始处理【启用默认地址】的业务，参数：{}", id);
        iAddressRepository.updateDefaultById(id, 1);
    }

    @Override
    public void updateDownDefaultById(Long id, Integer isDefault) {
        log.debug("开始处理【禁用默认地址】的业务，参数：{}", id);
        iAddressRepository.updateDefaultById(id, 0);
    }
}
